﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Windvoice.Blog;
using Windvoice.Framework;

namespace Windvoice.Areas.Manage.Controllers
{
    public class CategoryController : ManagerControllerBase
    {
        //
        // GET: /Manage/Category/
        private log4net.ILog DebugLogger = log4net.LogManager.GetLogger("debug");

        public ActionResult Index()
        {
            ViewBag.CategoryList = Category.FindAll();
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Category category)
        {
            DebugLogger.Info(category.ID);
            DebugLogger.Info(category.Name);
            DebugLogger.Info(category.Description);
            DebugLogger.Info(category.UniqueKey);
            DebugLogger.Info(ModelState.IsValid);

            if (ModelState.IsValid)
            {
                category.Create();
                return RedirectToAction("index");
            }
                return View(category);
        }

        public ActionResult Edit(int id)
        {
            return View(Category.Find(id));
        }

        [HttpPost]
        public ActionResult Edit(Category category)
        {
            if (ModelState.IsValid)
            {
                category.Update();
                return RedirectToAction("index");
            }
            return View();
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            if (id > 0)
            {
                Category.Find(id).Delete();
                return AjaxResult();
            }
            return AjaxResult(result: false, message: "参数错误！");
        }
    }
}
